﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ProtoGame.GameLib
{
    // FIX: Era uma struct de TileMap, agora é classe. Resolver questões pendentes de encapsulamento e coesão.
    public class Tileset
    {
        public readonly ushort TilesWidth;
        public readonly ushort TilesHeight;

        // Indica quantos Tiles de largura compõem o TileSet
        private readonly ushort TilesetWidth;

        // Indica quantos Tiles de altura compõem o TileSet
        private readonly ushort TilesetHeight;

        public Texture2D Texture;
        public int Count { get; private set; }

        /// <summary>
        /// Creates a mechanism for treating a texture like a tileset.
        /// </summary>
        /// <param name="texture">The source texture.</param>
        /*public Tileset(Texture2D texture)
            : this(texture, (ushort)texture.Width, (ushort)texture.Height)
        {
        }*/

        /// <summary>
        /// Creates a mechanism for treating a texture like a tileset.
        /// </summary>
        /// <param name="texture">The source texture.</param>
        /// <param name="tilesWidth">The width in pixels of a single tile. Can't be zero.</param>
        /// <param name="tilesHeight">The height in pixels of a single tile. Can't be zero.</param>
        public Tileset(Texture2D texture, ushort tilesWidth, ushort tilesHeight)
        {
            this.Texture = texture;
            this.TilesWidth = tilesWidth;
            this.TilesHeight = tilesHeight;

            this.TilesetWidth = (ushort)(Texture.Width / TilesWidth);
            this.TilesetHeight = (ushort)(texture.Height / tilesHeight);
            this.Count = TilesetWidth * TilesetHeight;
        }

        public void getTileRectangle(ushort tileNumber, out Rectangle tileRect)
        {
            getTileRectangle((ushort)(tileNumber % TilesetWidth), (ushort)(tileNumber / TilesetWidth), out tileRect);
        }

        public void getTileRectangle(ushort x, ushort y, out Rectangle tileRect)
        {
            tileRect.X = x * TilesWidth;
            tileRect.Y = y * TilesHeight;
            tileRect.Width = TilesWidth;
            tileRect.Height = TilesHeight;
        }

        public Rectangle getTileRectangle(ushort tileNumber)
        {
            Rectangle tileRect;
            getTileRectangle(tileNumber, out tileRect);

            return tileRect;
        }
    }
}
